package com.apiobject.framework.steps;

import com.apiobject.framework.global.ApiLoader;
import com.apiobject.framework.global.GlobalVariables;
import com.apiobject.utils.PlaceholderUtils;
import io.restassured.response.Response;
import lombok.Data;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.parallel.Execution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

@Data
public class StepModel {

    public static final Logger logger= LoggerFactory.getLogger(StepModel.class);

    private String api;
    private String action;
    private ArrayList<String> actualParameter;
    private ArrayList<AssertModel> asserts;
    private HashMap<String,String> save;
    private HashMap<String,String> saveGlobal;

    private ArrayList<String> finalActualParameter=new ArrayList<>();
    private HashMap<String,String> stepVariable=new HashMap<>();
    private ArrayList<Executable> assertList=new ArrayList<>();
    private StepResult stepResult=new StepResult();

    public StepResult run(HashMap<String,String> testCaseVariables){

        /**
         *1.实参占位符替換
         */
        if (actualParameter!=null){
            finalActualParameter.addAll(PlaceholderUtils.resolveList(actualParameter,testCaseVariables));
        }

        /**
         * 2.执行action
         */

        Response response= ApiLoader.getAction(api,action).run(finalActualParameter);

        /**
         * 3.存save
         */
        if (save!=null){
            save.forEach((variablesName,path)->{
                String value=response.path(path).toString();
                stepVariable.put(variablesName,value);
                logger.info("step變量更新："+stepVariable);
            });
        }

        /**
         * 4.存saveGlobal
         */
        if (saveGlobal!=null){
            saveGlobal.forEach((variablesName,path)->{
                String value=response.path(path).toString();
                GlobalVariables.getGlobalVariables().put(variablesName,value);
                logger.info("全局變量更新："+GlobalVariables.getGlobalVariables());
            });
        }

        /**
         * 5.存儲斷言結果
         */
        if (asserts!=null){
            asserts.stream().forEach(assertModel->{
//                assertList.add(()->{assertThat(assertModel.getReason(),response.path(assertModel.getActual()),equalTo(assertModel.getExpect()));});
            });
        }

        /**
         * 6.組裝result
         */
        stepResult.setAssertList(assertList);
        stepResult.setStepVariables(stepVariable);
        return stepResult;
    }

}
